package me.hyman.testcustomview.itemTouchHelper.channelsMgr;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import me.hyman.testcustomview.R;
import me.hyman.testcustomview.itemTouchHelper.helper.ItemTouchHelperCallback;

public class MyChannelsActivity extends AppCompatActivity {

    private RecyclerView recyclerView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_channels);

        init();
    }

    private void init() {

        recyclerView = (RecyclerView) findViewById(R.id.recyclerview);

        final List<Channel> myChannel = new ArrayList<>();
        List<Channel> otherChannel = new ArrayList<>();
        for (int i = 0; i < 20; i++) {
            Channel my = new Channel();
            my.setText("频道" + i);
            myChannel.add(my);

            Channel other = new Channel();
            other.setText("其他" + i);
            otherChannel.add(other);
        }

        ItemTouchHelperCallback callback = new ItemTouchHelperCallback();
        ItemTouchHelper helper = new ItemTouchHelper(callback);
        helper.attachToRecyclerView(recyclerView);

        final ChannelMgrAdapter adapter = new ChannelMgrAdapter(this, helper, myChannel, otherChannel);
        adapter.setOnMyChannelItemClickListener(new ChannelMgrAdapter.OnMyChannelItemClickListener() {
            @Override
            public void onItemClick(int position) {
                Toast.makeText(MyChannelsActivity.this, myChannel.get(position).getText(), Toast.LENGTH_SHORT).show();
            }
        });
        recyclerView.setAdapter(adapter);

        GridLayoutManager manager = new GridLayoutManager(this, 4);
        manager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                int viewType = adapter.getItemViewType(position);
                return (viewType == ChannelMgrAdapter.TYPE_MY_CHANNEL || viewType == ChannelMgrAdapter.TYPE_OTHER_CHANNEL) ? 1 : 4;
            }
        });

        recyclerView.setLayoutManager(manager);

    }
}
